package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.impl.RabbitmqServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

@RestController
@Api(tags = "rabbitmq测试消息发送测试接口")
@RequestMapping("/rabbitmq")
@Slf4j
public class SendRabbitMQDemoController {
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private RabbitmqServiceImpl rabbitmqService;

    @PostMapping("/multiSend")
    @ApiOperation(value = "多次发送消息", notes = "测试多次发送消息")
    public String multiSend() {
        StringBuilder times = new StringBuilder();
        rabbitTemplate.setReplyTimeout(10000);
        for (int i = 0; i < 100; i++) {
            long time = System.nanoTime();
            rabbitTemplate.convertAndSend("test2", "第" + i + "次发送的时间：" + time);

            times.append(time + "<br>");
        }
        return times.toString();
    }

    @PostMapping("/multi2MultiSend")
    @ApiOperation(value = "多次发送消息到多个队列", notes = "测试多次发送消息到多个队列")
    public String mutil2MutilSend() {
        StringBuilder times = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            long time = System.nanoTime();
//            amqpTemplate.convertAndSend("test1","第"+i+"次发送的时间："+time);
            rabbitTemplate.convertAndSend("test1", "第" + i + "次发送的时间：" + time);
            rabbitTemplate.convertAndSend("test1", User.builder().username("谭声俊").password("123456").build());

            rabbitTemplate.convertAndSend("test2", "第" + i + "次发送的时间：" + time);
            times.append(time + "<br>");
        }
        return times.toString();
    }

    //测试发送string类型的消息
    @PostMapping("/sendTest3")
    @ApiOperation(value = "发送test3队列的string类型的消息", notes = "测试发送test3队列的string类型的消息")
    public void sendTestString() {
        StringBuilder a = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            String uuid = UUID.randomUUID().toString();
            rabbitTemplate.convertAndSend("test3", "第" + i + "次发送的时间：" + uuid);
            a.append(uuid + "<br>");
        }
    }

    //测试发送int类型的消息
    @PostMapping("/sendTest33")
    public void sendTestInteger() {
        StringBuilder a = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("test3", 1);
        }
    }

    @PostMapping("/testQueueConcurrency")
    @ApiOperation(value = "测试队列并发", notes = "测试队列并发")
    public void testQueueConcurrency() {
        User build = User.builder().username("谭声俊").password("123456").build();
        rabbitTemplate.convertAndSend("work_zs_queue", build);
    }


    @PostMapping("/stopListener")
    @ApiOperation(value = "停止监听器")
    public void stopListener() {
        rabbitmqService.stopListener();
//            MessageListenerContainer listenerContainer = rabbitListenerEndpointRegistry.getListenerContainer(queueId);
    }
    @PostMapping("/startListener")
    @ApiOperation(value = "启动监听器")
    public void startListener() {
        rabbitmqService.startListener();
//            MessageListenerContainer listenerContainer = rabbitListenerEndpointRegistry.getListenerContainer(queueId);
    }

    @PostMapping("/testNormal")
    @ApiOperation(value = "测试普通无代码接口压测是否会发生错误")
    public void testNormalInsert() {
    }

}